//
//  CircleSecondLevelViewController.m
//  安好
//
//  Created by rlp on 14-11-24.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "CircleSecondLevelViewController.h"

@interface CircleSecondLevelViewController ()

@end

@implementation CircleSecondLevelViewController

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[AFHTTPRequestOperationManager manager].operationQueue cancelAllOperations];
    [self hiddenActionView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.secondCircleArray = [[NSMutableArray alloc]initWithCapacity:10];
    // Do any additional setup after loading the view from its nib.
    [self loadCircleData];
    [self makeNavBarView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    //隐藏系统的cell之间的分割线
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:self.tableView];
}
#pragma mark - 设置导航条
-(void)makeNavBarView
{
    self.navigationItem.title = [NSString stringWithFormat:@"%@",self.circleModelData.title];
    //贴图
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClicked:)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
 
}

-(void)leftButtonClicked:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)loadCircleData{
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setObject:@"1" forKey:@"page"];
    [dict setObject:[NSString stringWithFormat:@"%@",self.circleModelData.gid] forKey:@"pid"];
    [dict setObject:@"20" forKey:@"pagesize"];
    
    
    
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,GROUP_LIST] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("%@",responseObject);
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            alertContent([responseObject objectForKey:@"msg"]);
        }else{
            [self.secondCircleArray removeAllObjects];
            for (int i=0; i<[[responseObject objectForKey:@"data"] count]; i++) {
                GroupUserModel *groupUserModel = [GroupUserModel modelObjectWithDictionary:[[responseObject objectForKey:@"data"] objectAtIndex:i]];
                [self.secondCircleArray addObject:groupUserModel];
            }
            [self.tableView reloadData];
            
        }
        [self hiddenActionView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];

    }];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger number=0;
  
        number = 1;
    return number;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSInteger number=0;
 
        number = self.secondCircleArray.count;
      return number;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger hight=CIRCLE_SECOND_LEVEL_TABLEVIEWCELL_HIGHT;
    return hight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    static NSString *ID=nil;
    CircleSecondLevelTableViewCell *cell = nil;
    if (kScreenWidth == 414) {
        ID=@"tableViewCell414";
    }else{
        ID=@"tableViewCell";
    }
    cell = [tableView dequeueReusableCellWithIdentifier:ID];

    
        if (cell == nil) {
            if (kScreenWidth == 414) {

                    cell = [[[NSBundle mainBundle] loadNibNamed:@"CircleSecondLevelTableViewCell" owner:self options:nil] lastObject];
            }else{
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CircleSecondLevelTableViewCell" owner:self options:nil] firstObject];

            }
            
            /**
             *  线条的处理
             */
            
            UIView *bottomLineView = [[UIView alloc]init];
            
            if (indexPath.row != self.secondCircleArray.count - 1)
            {
                bottomLineView.frame = CGRectMake(70, cell.contentView.height-0.5,kScreenWidth-70 - 5, 0.5);
            }
            else
            {
                bottomLineView.frame = CGRectMake(0, cell.contentView.height-0.5, kScreenWidth, 0.5);
            }
            
            bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
            [cell.contentView addSubview:bottomLineView];


        }
    
    
    CircleModelData *circleMdata =[self.secondCircleArray objectAtIndex:indexPath.row];
      // Configure the cell...
    [cell.icon setImageURLStr:circleMdata.icon placeholder:[UIImage imageNamed:@"公用_默认图片"]];
    
    [(CircleSecondLevelTableViewCell *)cell title].text = [[self.secondCircleArray objectAtIndex:indexPath.row] title];
    [(CircleSecondLevelTableViewCell *)cell subtitle].text =[[self.secondCircleArray objectAtIndex:indexPath.row] lastposttitle];
    
    [[(CircleSecondLevelTableViewCell *)cell thread] setTitle:[NSString stringWithFormat:@" %@",[[self.secondCircleArray objectAtIndex:indexPath.row] threads]] forState:UIControlStateNormal];
    [[(CircleSecondLevelTableViewCell *)cell post] setTitle:[NSString stringWithFormat:@" %@",[[self.secondCircleArray objectAtIndex:indexPath.row] posts]] forState:UIControlStateNormal];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CirclePostsDetailsViewController *cplVC = [[CirclePostsDetailsViewController alloc]initWithNibName:@"CirclePostsDetailsViewController" bundle:nil];
    cplVC.groupUserModel =[self.secondCircleArray objectAtIndex:indexPath.row];
    MyNSLog("%@",[self.secondCircleArray objectAtIndex:indexPath.row]);
    [self.navigationController pushViewController:cplVC animated:YES];
       
}


@end
